<?php defined('SYSPATH') OR die('No direct access allowed.');

class WB_Template {
	
	private $template = '';
	private $base_url = '';
	private $base_path = '';
	
	private $css = array();
	private $js = array();
	private $js_var = array();
	private $script = array();
	
	private $css_ie = array();
	private $link = array();
	
	public function __construct()
	{
		$this->template = Kohana::config('admin.template');
		$this->base_url = url::base('').Kohana::config('admin.assets');
		$this->base_path = DOCROOT.Kohana::config('admin.assets');
	}
	
	public function clear_all()
	{
		$this->css = array();
		$this->js = array();
		$this->js_var = array();
		$this->script = array();
		$this->css_ie = array();
		$this->link = array();
	}
	
	/**
	 * Adds a css file to the header
	 * 
	 * @param string id
	 * @param string filename
	 * @param string internet explorer version
	 * @param media screen
	 * @return void
	 */
	public function css($id, $filename, $media='screen')
	{
		$this->css[$media][$id] = strpos($filename, '://') ? $filename : $this->get_filename('css/'.$filename);
	}
	
	/**
	 * Adds a css file to the header
	 * 
	 * @param string id
	 * @param string filename
	 * @param string|integer|float expr (http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx)
	 * @param media screen
	 * @return void
	 */
	public function css_ie($id, $filename, $expr='lte IE 7', $media='screen')
	{
		$expr = is_numeric($expr) ? 'lte IE '.$expr : $expr;
		$this->css[$expr][$media][$id] = strpos($filename, '://') ? $filename : $this->get_filename('css/'.$filename);
	}
	
	/**
	 * Adds a js file to the header
	 * 
	 * @param string id
	 * @param string filename
	 * @return void
	 */
	public function js($id, $filename=null, $ext='.js', $randomize=FALSE)
	{
		$filename = empty($filename) ? $id : $filename;
		$filename = empty($ext) ? $filename : $filename.$ext;
		$filename = $randomize===TRUE ? $filename.'?rand='.rand(1000,10000) : $filename;
		$this->js[$id] = strpos($filename, '://') ? $filename : $this->get_filename('js/'.$filename);
		
	}
	
	/**
	 * Add a javascript variable
	 * 
	 * @param $var
	 * @param $value
	 * @return void
	 */
	public function js_var($var, $value=null)
	{
		if (is_array($var)) foreach ($var as $k=>$v) $this->js_var[$k] = $v;
		else $this->js_var[$var] = $value;
	}
	
	/**
	 * Add a script to the header
	 * 
	 * @param $id
	 * @param $script
	 * @return void
	 */
	public function script($id, $script)
	{
		$this->script[$id] = $script;
	}

	/**
	 * Creates a link tag.
	 *
	 * @param   string|array  filename
	 * @param   string|array  relationship
	 * @param   string|array  mimetype
	 * @param   string        specifies suffix of the file
	 * @param   string|array  specifies on what device the document will be displayed
	 * @param   boolean       include the index_page in the link
	 * @return  string
	 */
	public function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
	{
		$this->link[] = func_get_args();
	}

	/**
	 * Creates an image link.
	 *
	 * @param   string        image source, or an array of attributes
	 * @param   string|array  image alt attribute, or an array of attributes
	 * @param   boolean       include the index_page in the link
	 * @return  string
	 */
	public function image($src = NULL, $alt = NULL, $index = FALSE)
	{
		// Create attribute list
		$attributes = is_array($src) ? $src : array('src' => $src);

		if (is_array($alt))
		{
			$attributes += $alt;
		}
		elseif ( ! empty($alt))
		{
			// Add alt to attributes
			$attributes['alt'] = $alt;
		}

		if (strpos($attributes['src'], '://') === FALSE)
		{
			// Make the src attribute into an absolute URL
			$attributes['src'] = $this->get_filename($attributes['src']);
		}

		return '<img'.html::attributes($attributes).' />';
	}
	
	/**
	 * Check for template version of the asset
	 * 
	 * @param $filename
	 * @return void
	 */
	public function get_filename($filename)
	{
		if (strpos($filename, ':')) return $filename;
		/*
		if (file_exists(str_replace('\\', '/',$this->base_path.$this->template.'/'.$filename)))
			die (str_replace('\\', '/',$this->base_path.$this->template.'/'.$filename));
		else
			die (str_replace('\\', '/',$this->base_path.'common/'.$filename));
		*/
		return file_exists($this->base_path.$this->template.'/'.$filename) ?
			$this->base_url.$this->template.'/'.$filename :
			$this->base_url.'common/'.$filename;
	}
	
	/**
	 * Render page header
	 * 
	 * @return void
	 */
	public function render()
	{
		$return = '';
		foreach ($this->css as $media=>$css) $return .= html::stylesheet($css, $media);
		
		if ( ! empty($css_ie))
		{
			foreach ($this->css_ie as $expression=>$css)
			{
				$return .= "<!--[if lte IE 7]>";
				foreach ($this->css_ie as $media=>$css) $return .= html::stylesheet($css, $media);
				$return .= "<![endif]-->";
			}
		}
		
		foreach ($this->link as $link) $return .= html::link($link[0], $link[1], $link[2], $link[3], $link[4], $link[5]);
		
		foreach ($this->js as $js) $return .= "<script type=\"text/javascript\" src=\"".$js."\"></script>\n";
		
		if ( ! empty($this->script) || ! empty($this->js_var))
		{
			$return .= "\n<script type=\"text/javascript\">\n";
			foreach ($this->js_var as $k=>$js) $return .= 'var '.$k.' = '.json_encode($js)."\n";
			foreach ($this->script as $js) $return .= $js."\n";
			$return .= "</script>\n";
		}
		
		return $return;
	}

	/**
	 * Magically converts view object to string.
	 *
	 * @return  string
	 */
	public function __toString()
	{
		try
		{
			return $this->render();
		}
		catch (Exception $e)
		{
			// Display the exception using its internal __toString method
			return (string) $e;
		}
	}
}

//End of WB_Template Library